import socket
from DesModule import des_encrypt, des_descrypt

TCP_IP = "192.168.1.111"
TCP_PORT = 2028
KEY = "12345678"

tcpserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpserver.bind((TCP_IP, TCP_PORT))
tcpserver.listen(5)

def receive_encrypted_data(conn, KEY):
    encrypted_data = conn.recv(1024).decode()
    decrypted_data = des_descrypt(encrypted_data, KEY)
    return decrypted_data

while True:
    conn, addr = tcpserver.accept()
    print(conn)
    while True:
        try:
            data = receive_encrypted_data(conn, KEY)
            print(data)
            ret_data = input("请回复：")
            encrypted_ret_data = des_encrypt(ret_data, KEY)
            conn.send(encrypted_ret_data.encode())
        except Exception:
            break
    conn.close()